class CategoriesController < ApplicationController
  before_filter :check_administrator_role,:except=>[:show]
 
  def index
    @categories = Category.paginate :page => params[:page],:per_page => 15

    respond_to do |format|
      format.html # index.html.erb
      format.xml  {render :xml => @categories.to_xml}
    end
  end

  def show
    @category = Category.find(params[:id])
    if params[:brand_id]
      @sub_category = Category.find_by_parent_and_brand(params[:brand_id],params[:id])
      @brand = Brand.find(params[:brand_id])
      @products = Product.paginate_by_sql(["SELECT p.*
FROM `products` p, categories c
WHERE ((p.category_id = c.id and c.id = ?)
OR ((c.parent_id = ?)
AND p.category_id = c.id))
AND (DATE_ADD(p.active_date,INTERVAL p.live_date DAY) > now())
AND p.brand_id = ?
order by p.sort_order desc",params[:id],params[:id],params[:brand_id]],
        :page => params[:page],:per_page => 5)
    else
      @category = Category.find(params[:id])
      @sub_category = @category.children
      @brands = Brand.get_all_by_categery(params[:id])
      @products = Product.paginate_by_sql(["SELECT p.*
FROM `products` p, categories c
WHERE ((p.category_id = c.id and c.id = ?)
OR ((c.parent_id = ?)
AND p.category_id = c.id))
And (DATE_ADD(p.active_date,INTERVAL p.live_date DAY) > now())
order by p.sort_order desc",params[:id],params[:id]],:page => params[:page],:per_page => 5)
    end

    respond_to do |format|
      format.html
      format.xml  { render :xml => @category}
    end
  end

  def new
    @category = Category.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.xml
  def create
    @category = Category.new(params[:category])

    respond_to do |format|
      if @category.save
        flash[:notice] = 'Category was successfully created.'
        format.html { redirect_to(categories_url) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:id])
    result = @category.update_attributes(params[:category])


    if !request.xhr?
      respond_to do |format|
        if result
          flash[:notice] = 'Category was successfully updated.'
          format.html { redirect_to(categories_url) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
        end
      end
    else
      if result
        render :text=> params[:category].values.to_s
      else
        render :text=>'false'
      end
    end
    
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
    end
  end

  def migrate_product
    new_category_id = params[:new_category_id]
    new_category = Category.find(new_category_id)
    old_category = Category.find(params[:id])

    if new_category_id
      result = Product.update_all(["category_id = ? where category_id = ?",new_category_id,params[:id]])
      Category.update_counters  params[:new_category_id],:products_count=>old_category.products.size
      Category.update_counters  params[:id],:products_count=>-old_category.products.size
    end


    respond_to do |format|
      if result
        flash[:notice] = "成功更新#{result}条记录."
        format.html { redirect_to(new_category) }
        format.xml  { head :ok }
      else
        format.html { redirect_to(new_category) }
        format.xml  { render :xml => category.errors, :status => :unprocessable_entity }
      end
    end
  end


  def sort_product
    if params[:brand_id]
      @brand=Brand.find(params[:brand_id])
      @category = Category.find(params[:id])
      @products = Product.find_all_by_brand_id_and_category_id(params[:brand_id],params[:id],:order=>'sort_order desc')
    else
      @products = Product.find_all_by_category_id(params[:id],:order=>'sort_order desc')
    end
  end

end
